﻿using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;

namespace EasyXaf.EntityDesigner.Commands;

public class CreateAssociationCommand : ICommand
{
    public bool CanExecute(string command)
    {
        var fragments = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        if (fragments.Length == 3)
        {
            return fragments[1] == ">";
        }
        return false;
    }

    public void Execute(string command, CommandContext context)
    {
        var fragments = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        var source = fragments[0];
        var target = fragments[2];

        Entity sourceEntity = null;
        EntityProperty sourceProperty = null;

        Entity targetEntity = null;
        EntityProperty targetProperty = null;

        if (source.Contains('.'))
        {
            var sourceEntityName = source.Split('.')[0];
            var sourcePropertyName = source.Split('.')[1];
            sourceEntity = context.DomainModel.VisualEntities.FirstOrDefault(e => e.Name.Equals(sourceEntityName, StringComparison.OrdinalIgnoreCase));
            if (sourceEntity != null)
            {
                sourceProperty = sourceEntity.Properties.FirstOrDefault(e => e.Name.Equals(sourcePropertyName, StringComparison.OrdinalIgnoreCase));
                if (sourceProperty == null)
                {
                    throw new CommandException(command, $"没有找到原实体属性 '{sourcePropertyName}'");
                }
            }
            else
            {
                throw new CommandException(command, $"没有找到原实体 '{sourceEntityName}'");
            }
        }
        else
        {
            sourceEntity = context.DomainModel.VisualEntities.FirstOrDefault(e => e.Name.Equals(source, StringComparison.OrdinalIgnoreCase));
            if (sourceEntity == null)
            {
                throw new CommandException(command, $"没有找到原实体 '{source}'");
            }
        }

        if (target.Contains('.'))
        {
            var targetEntityName = target.Split('.')[0];
            var targetPropertyName = target.Split('.')[1];
            targetEntity = context.DomainModel.VisualEntities.FirstOrDefault(e => e.Name.Equals(targetEntityName, StringComparison.OrdinalIgnoreCase));
            if (targetEntity != null)
            {
                targetProperty = targetEntity.Properties.FirstOrDefault(p => p.Name.Equals(targetPropertyName, StringComparison.OrdinalIgnoreCase));
                if (targetProperty == null)
                {
                    throw new CommandException(command, $"没有找到目标实体属性 '{targetPropertyName}'");
                }
            }
            else
            {
                throw new CommandException(command, $"没有找到目标实体 '{targetEntityName}'");
            }
        }
        else
        {
            targetEntity = context.DomainModel.VisualEntities.FirstOrDefault(e => e.Name.Equals(target, StringComparison.OrdinalIgnoreCase));
            if (targetEntity == null)
            {
                throw new CommandException(command, $"没有找到目标实体 '{target}'");
            }
        }

        sourceEntity.CreateAssociation(sourceEntity, sourceProperty, targetEntity, targetProperty);
    }
}
